package com.feng.entity;

import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.JSONWriter;

/**
 *
 * @author feng
 *
 * 同一响应格式
 *
 * **/
public record RestResponse<T>(int code, T data, String message) {

    public static <T>  RestResponse<T> success(T data) {
        return new RestResponse<>(200, data, "success");
    }

    public static <T>  RestResponse<T> success() {
        return success(null);
    }

    public static <T> RestResponse<T> failure(int code, String message) {
        return new RestResponse<>(code, null, message);
    }

    public String asJsonString(){
        return JSONObject.toJSONString(this, JSONWriter.Feature.WriteNulls);
    }

}
